package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     *
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 新增菜品
     * 新增一条记录后 让mybatis把这条记录的id值返回回来
     * 执行成功后把id值放到参数里面的某个属性身上去,使用数据库自增的id
     *
     * @param dish
     */
    @AutoFill(OperationType.INSERT)
    @Options(keyProperty = "id", useGeneratedKeys = true)
    @Insert("insert into sky_take_out.dish values (null,#{name},#{categoryId},#{price},#{image},#{description},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser}) ")
    void add(Dish dish);

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    Page<DishVO> page(DishPageQueryDTO dishPageQueryDTO);

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Select("select * from sky_take_out.dish where id=#{id} ")
    Dish findById(Long id);

    /**
     * 根据id删除菜品
     *
     * @param id
     */
    @Delete("delete from sky_take_out.dish where id=#{id}")
    void deleteById(Long id);

    /**
     * 修改菜品信息
     *
     * @param dish
     */
    @AutoFill(OperationType.UPDATE)
    @Options(keyProperty = "id", useGeneratedKeys = true)
    void update(Dish dish);

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     */
    @Update("update sky_take_out.dish set status = #{status} where id=#{id}")
    void updateStatus(Integer status, Long id);

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Select("select * from sky_take_out.dish  where category_id=#{cetegoryId}")
    List<Dish> findByCategoryId(Long categoryId);

    /**
     * 条件查询菜品
     * @param dish
     * @return
     */
    @Select("select * from sky_take_out.dish where category_id=#{categoryId} and status=#{status} ")
    List<Dish> list(Dish dish);

    /**
     * 根据条件统计菜品数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
